package com.kandinfo.survey.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kandinfo.base.common.constant.ResultCode;
import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.base.security.SecurityContext;
import com.kandinfo.base.security.domain.User;
import com.kandinfo.survey.common.domain.UserAnswerInfo;
import com.kandinfo.survey.common.dto.useranswer.UserAnswerRequestDto;
import com.kandinfo.survey.common.service.UserAnswerDetailService;
import com.kandinfo.survey.common.service.UserAnswerInfoService;
import com.kandinfo.survey.common.service.UserAnswerService;
import com.kandinfo.survey.common.service.UserQuestionnaireRelaService;

@Service
public class UserAnswerServiceImpl implements UserAnswerService {

    @Autowired
    UserAnswerInfoService userAnswerInfoService;
    @Autowired
    UserAnswerDetailService userAnswerDetailService;
    @Autowired
    UserQuestionnaireRelaService userQuestionnaireRelaService;

    @Override
    public void saveRela(UserAnswerRequestDto requestDto) {
        userQuestionnaireRelaService.saveRela(requestDto.getQuestionnaireId());
    }
    
    @Override
    public ResultDto deleteRela(UserAnswerRequestDto requestDto) {
        Boolean delFlag = userQuestionnaireRelaService.deleteRela(requestDto.getQuestionnaireId());

        if (delFlag) {
            return ResultDtoFactory.createResult(ResultCode.SUCCESS, "删除成功!", Boolean.TRUE);
        } else {
            return ResultDtoFactory.createResult(ResultCode.ERROR, "删除失败!", Boolean.FALSE);
        }
    }

    @Override
    public void saveData(UserAnswerRequestDto requestDto) {
        // get user
        User curUser = SecurityContext.getInstance().getCurrentUser();
        // save info
        UserAnswerInfo info = userAnswerInfoService.saveUserAnswerInfo(curUser, requestDto.getQuestionnaireId());
        // save detail(need infoId)
        userAnswerDetailService.saveUserAnswerDetail(curUser, requestDto, info);
    }

}
